package com.wsoft.system.controller;

import com.wsoft.constant.RedisKeyConstant;
import com.wsoft.core.service.RedisService;
import com.wsoft.response.ResponsePage;
import com.wsoft.response.ResponseResult;
import com.wsoft.system.query.dict.TsDictQuery;
import com.wsoft.system.query.dict.TsDictReq;
import com.wsoft.system.service.ITsDictDataService;
import com.wsoft.system.service.ITsDictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 数据字典表 前端控制器
 * </p>
 *
 * @author kyle
 * @since 2024年08月29日 14:37:48
 */
@RestController
@Tag(name = "数据字典")
@RequestMapping("/ts/dict")
public class TsDictController {
    @Resource
    private ITsDictDataService dictDataService;
    @Resource
    private RedisService redisService;

    @Resource
    private ITsDictService dictService;

    @Operation(summary = "获取字典数据",description = "获取字典数据")
    @GetMapping("/getDataList")
    public ResponseResult getDataList(@RequestParam("id") Long id) {
        return ResponseResult.success(dictDataService.getDictDataList(id));
    }


    @Operation(summary = "新增字典",description = "新增字典")
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody @Valid TsDictReq req) {
        boolean b = dictService.insert(req);
        if (b){
            return ResponseResult.success("[sys.msg.success]");
        }
        else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "编辑字典",description = "编辑字典")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Valid TsDictReq req) {
        boolean b = dictService.update(req);
        if (b){
            //清除缓存
            redisService.del(RedisKeyConstant.DICT_CACHE+req.getCode());
            return ResponseResult.success("[sys.msg.success]");
        }
        else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "删除字典",description = "删除数据")
    @GetMapping("/delete")
    public ResponseResult delete(@RequestParam("id") Long id) {
        boolean b = dictService.delete(id);
        if (b){

            return ResponseResult.success("[sys.msg.success]");
        }
        else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }
    @Operation(summary = "查看字典详情",description = "查看字典详情")
    @GetMapping("/get")
    public ResponseResult getDictById(@RequestParam("id") Long id) {
        return ResponseResult.success(dictService.getDict(id));
    }


    @Operation(summary = "字典列表分页",description = "字典列表分页")
    @PostMapping("/list")
    public ResponseResult pageList(@RequestBody @Valid TsDictQuery req) {
        return ResponseResult.success(ResponsePage.restPage(dictService.pageDict(req)));
    }
}
